package com.github.pig.admin.controller.zhgl.propertyassess;

import com.github.pig.admin.controller.SysCommunityUserController;
import com.github.pig.admin.model.zhgl.propertyassess.AssessDetaiEntity;
import com.github.pig.admin.model.zhgl.propertyassess.PropertyAssessCount;
import com.github.pig.admin.service.sys.SysUserService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.common.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.baomidou.mybatisplus.plugins.Page;
import javax.servlet.http.HttpServletRequest;
import com.github.pig.admin.model.zhgl.propertyassess.PropertyAssess;
import com.github.pig.admin.service.zhgl.propertyassess.IPropertyAssessService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物业人员评价表 前端控制器
 * </p>
 *
 * @author xiaosa
 * @since 2018-06-11
 */
@RestController
@RequestMapping("/propertyAssess")
public class PropertyAssessController extends BaseController {
    @Autowired
    private IPropertyAssessService propertyAssessService;
    @Autowired
    private SysCommunityUserController sysCommunityUserController;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return PropertyAssess
     */
    @GetMapping("/{id}")
    public R<PropertyAssess> get(@PathVariable Integer id) {
        return new R<>(propertyAssessService.selectById(id));
    }

    @RequestMapping(params = "list")
    public String toIndex(ModelMap modelMap, HttpServletRequest request){
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
        modelMap.addAttribute("basePath",basePath);
        return "zhgl/propertyassess/propertyAssessList";
    }

    /**
     * 获取评价详情列表
     * @param params
     * @param propertyId
     * @return
     */
    @RequestMapping("/detailPage")
    @ResponseBody
    public Page detailPage(@RequestParam Map<String, Object> params, Integer propertyId,String assessType,String communityId) {

        Page<AssessDetaiEntity> page = new Page<AssessDetaiEntity>(Integer.parseInt(params.getOrDefault("page", 1).toString())
                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
        return propertyAssessService.detailPage(page,propertyId,assessType,communityId);
    }

    /**
     * 关联查询
     *
     * @return PropertyAssess
     */
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Page selectTotal(@RequestParam Map<String, Object> params,PropertyAssessCount propertyAssessCount) {
        Map<String, Object> condition=new HashMap<String, Object>();
        Page<PropertyAssessCount> page = new Page<PropertyAssessCount>(Integer.parseInt(params.getOrDefault("page", 1).toString())
                , Integer.parseInt(params.getOrDefault("limit", 10).toString()));
        // 获取到当前登录用户
        Claims claims = getCurrentUserInfo();
        String currentUserName =  (String) claims.get("user_name");
        UserVO userVO = sysUserService.findUserByUsername(currentUserName);
        if(!CommonConstant.MANAGE_NAME.equalsIgnoreCase(currentUserName)){
            // 获取到当前登录用户id
            Integer userId = userVO.getUserId();
            propertyAssessCount.setUserId(userId);
        }
        return propertyAssessService.selectTotal(page,propertyAssessCount);
    }

    /**
     * 添加
     * @param  propertyAssess  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody PropertyAssess propertyAssess) {
        return new R<>(propertyAssessService.insert(propertyAssess));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        PropertyAssess propertyAssess = new PropertyAssess();
        propertyAssess.setId(id);
        propertyAssess.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(propertyAssessService.updateById(propertyAssess));
    }

    /**
     * 编辑
     * @param  propertyAssess  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody PropertyAssess propertyAssess) {
        return new R<>(propertyAssessService.updateById(propertyAssess));
    }

    /**
     * 根据当前登录用户所属小区获取到对应小区下所有物业人员评价总数
     *
     * @return success/false
     */
    @RequestMapping("/sumComments")
    public AjaxJson getSumComments(PropertyAssessCount propertyAssessCount){

        // 获取到当前登录用户
        Claims claims = getCurrentUserInfo();
        String currentUserName =  (String) claims.get("user_name");
        UserVO userVO = sysUserService.findUserByUsername(currentUserName);
        // 获取到用户id
        Integer userId = userVO.getUserId();
        propertyAssessCount.setUserId(userId);
        return propertyAssessService.getSumComments(propertyAssessCount);
    }
}
